// infocenter.js
import api from "../../utils/api"
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userHead:"../../img/userhead.png",
    headbg:"../../img/infobg.png",
    alertTextBox:{
      show:false,
      title:"系统提示",
      message:"",
      maxLength:10
    },
    alertTextBox1: {
      show: false,
      title: "系统提示",
      message: "",
      maxLength: 60
    },
    userInfo:{
      sign:"",//个性签名
      nickname:""//用户名
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    /**
     * 加载个人信息
     */
    var that = this;
    wx.request({
      url: api.getUserById,
      data:{
        id:app.globalData.openid
      },
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      success:function(response){
        console.log("获取个人信息成功！");
        app.globalData.userHead = response.data.data[0].head;
        app.globalData.userName = response.data.data[0].nickname;
        that.setData({
          userInfo: response.data.data[0]
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 打开地理位置
   * latitude
:
30.274085
longitude
:
120.15507
   */
  showLocation:function(){
    var that = this;
    wx.getLocation({
      success: function(res) {
        console.log("获取地理位置");
        console.log(res);
        debugger;
        wx.openLocation({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: [{
            iconPath: "../../img/map_icon2.png",
            id: 0,
            latitude: 23.099954,
            longitude: 113.326720,
            width: 20,
            height: 30
          },
          {
            iconPath: "../../img/map_icon3.png",
            id: 0,
            latitude: 23.099984,
            longitude: 113.324542,
            width: 20,
            height: 30
          },
          {
            iconPath: "../../img/map_icon3.png",
            id: 0,
            latitude: 23.098984,
            longitude: 113.323542,
            width: 20,
            height: 30
          },
          {
            iconPath: "../../img/map_icon3.png",
            id: 0,
            latitude: 23.095984,
            longitude: 113.320542,
            width: 20,
            height: 30
          },
          {
            iconPath: "../../img/map_icon3.png",
            id: 0,
            latitude: 23.100984,
            longitude: 113.334542,
            width: 20,
            height: 30
          }]
        })
      },
    })
  },
  /**
   * 更换头像
   */
  changeHeadImg:function(){
    var that = this;
    wx.chooseImage({
      count:1,
      success: function(res) {
        // debugger;
        console.log(res.tempFiles[0].path);
        that.data.userInfo.head = res.tempFiles[0].path;
        that.setData({
          userInfo: that.data.userInfo
        });
        wx.uploadFile({
          header:{
            "content-type": "multipart/form-data"
          },
          url: api.insertHeadImg,
          filePath: res.tempFiles[0].path,
          name: 'uploadFile',
          formData:{
            id:app.globalData.openid
          },
          success:function(response){
            console.log("上传文件成功！");
            console.log(response);
            // that.data.userInfo.head = 
            that.setData();
          }
        })
      },
    })
  },
  /**
   * 更换背景图
   */
  changeBgImg:function(){
    var that = this;
    wx.chooseImage({
      count:1,
      success: function (res) {
        console.log(res);
        that.setData({
          headbg: res.tempFiles[0].path
        });
      },
    })
  },
  /**
   * 修改昵称
   */
  changeUserName:function(){
    this.showAlertTextBox({});
  },
  /**
   * 修改签名
   */
  changeOverride:function(){
    this.showAlertTextBox1({});
  },
  /**
   * 打开附近的人
   */
  openMapNear:function(){
    this.showLocation();
  },
  /**
   * 修改昵称、签名时弹出框
   */
   showAlertTextBox:function () {
     this.data.alertTextBox.show = true;
     this.setData({
      alertTextBox:this.data.alertTextBox
     });
   },
   changeAlertInputMessage:function (e) {
     console.log(e.detail.value);
     this.data.alertTextBox.message = e.detail.value;
     this.setData({
      alertTextBox:this.data.alertTextBox
     });
   },
   cancelTextBox:function(){
     this.data.alertTextBox.show = false;
     this.setData({
       alertTextBox: this.data.alertTextBox
     });
   },
   SubmitTextBox:function(e){
     var that = this;
     this.data.alertTextBox.show = false;
     this.data.userInfo.nickname = this.data.alertTextBox.message;

     this.data.alertTextBox.show = false;
     this.setData({
       userInfo: this.data.userInfo,
       alertTextBox: this.data.alertTextBox
     });
     //提交昵称
     wx.request({
       url: api.updateNickname,
       data:{
         id:app.globalData.openid,
         nickname:that.data.userInfo.nickname
       },
       header:{
         "content-type": "application/x-www-form-urlencoded"
       },
       success:function(response){
          console.log("修改昵称成功！");
          console.log(response.data.data);
       },
       method:"POST"
     })
   },
   /**
   * 修改签名时弹出框
   */
   showAlertTextBox1: function () {
     this.data.alertTextBox1.show = true;
     this.setData({
       alertTextBox1: this.data.alertTextBox1
     });
   },
   changeAlertInputMessage1: function (e) {
     console.log(e.detail.value);
     this.data.alertTextBox1.message = e.detail.value;
     this.setData({
       alertTextBox1: this.data.alertTextBox1
     });
   },
   cancelTextBox1: function () {
     this.data.alertTextBox1.show = false;
     this.setData({
       alertTextBox1: this.data.alertTextBox1
     });
   },
   SubmitTextBox1: function (e) {
     var that = this;
     console.log(this.data.alertTextBox1);
     this.data.userInfo.sign = that.data.alertTextBox1.message;
     //  this.data.alertTextBox.
     this.data.alertTextBox1.show = false;
     this.setData({
       alertTextBox1: this.data.alertTextBox1,
       userInfo:that.data.userInfo
     });
     //修改签名
     wx.request({
       url: api.updateUserById,
       data:{
         id:app.globalData.openid,
         sign: this.data.userInfo.sign
       },
       header:{
         "content-type": "application/x-www-form-urlencoded"
       },
       success:function(response){
          console.log("修改签名成功！");
          console.log(response.data.data);
       },
       method:"POST"
     })
   },
   /**
    * 啥也不干
    */
   shayebugan:function(){
     
   }
})